﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Violation_Index : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            
            if (Session["Permission"] != null)
            {
                Permission permission = Session["Permission"] as Permission;
                if (!permission.IsUserEdit)
                {
                    
                }

                Bind(permission);


            }
        }
    }

    void Bind(Permission permission)
    {
        ViolationNoticeBLL vbll = new ViolationNoticeBLL(permission);
        string condition = "";
        if (!string.IsNullOrEmpty(txtKeyWord.Text))
        {
            condition += vbll.GetKeyWordCondition(txtKeyWord.Text);
        }

        if (!string.IsNullOrEmpty(txtSearchDate.Text))
        {
            condition += vbll.GetCommitDateCondition(txtSearchDate.Text);
        }


        switch (permission.EUserRole)
        {
            case EUserRole.superadmin:
                gvViolation.DataSource = vbll.GetAllViolationNoticeView(condition).Tables[0];
                gvViolation.DataBind();
                gvViolation.Columns[11].Visible = true;
                gvViolation.Columns[12].Visible = false;
                break;

            case EUserRole.admin:
                gvViolation.Columns[11].Visible = false;
                gvViolation.Columns[12].Visible = true;
                gvViolation.DataSource = vbll.GetAllViolationNoticeView(condition).Tables[0];
                gvViolation.DataBind();
                break;

            case EUserRole.leader://查看分配的
                gvViolation.Columns[11].Visible = false;
                gvViolation.Columns[12].Visible = false;
                gvViolation.DataSource = vbll.GetAllViolationNoticeView("and leaderid=" + permission.UserID.ToString() + condition).Tables[0];
                gvViolation.DataBind();
                break;

            case EUserRole.user://查看自己的
                gvViolation.Columns[11].Visible = false;
                gvViolation.Columns[12].Visible = false;
                gvViolation.DataSource = vbll.GetAllViolationNoticeView("and userid=" + permission.UserID.ToString() + condition).Tables[0];
                gvViolation.DataBind();
                break;
            default:
                gvViolation.Columns[11].Visible = false;
                gvViolation.Columns[12].Visible = false;
                break;
        }
    }

    protected void gvViolation_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    protected void gvViolation_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        if (Session["Permission"] != null)
        {
            Permission permission = Session["Permission"] as Permission;
            if (!permission.IsViolationNoticeEdit)
            {

                return;
            }
            string uid = gvViolation.DataKeys[e.RowIndex].Value.ToString();//获取主键  
            ViolationNoticeBLL vbll = new ViolationNoticeBLL(permission);
            vbll.DeleteViolationNotice(Convert.ToInt32(uid));
            Response.Write("<script> alert( '删除成功！ ');location= 'Index.aspx'</script> ");
        }
    }

    protected void gvViolation_RowEditing(object sender, GridViewEditEventArgs e)
    {
        

    }

    protected void gvViolation_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        if (Session["Permission"] != null)
        {
            Permission permission = Session["Permission"] as Permission;

            gvViolation.PageIndex = e.NewPageIndex;
            Bind(permission);
        }
    }

    protected void gvViolation_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (Session["Permission"] != null)
        {
            Permission permission = Session["Permission"] as Permission;
            if (e.CommandName == "Edit" && permission.IsViolationNoticeEdit)
            {
                string vid = ((HiddenField)gvViolation.Rows[Convert.ToInt32(e.CommandArgument)].FindControl("hID")).Value;
                Response.Redirect("Edit.aspx?vid=" + vid);
            }

            if (e.CommandName == "Deduct" && permission.IsDeductOperate)//扣分
            {
                //选中即跳往相关人员列表
              string []arr = e.CommandArgument.ToString().Split('|');
              Response.Redirect("~/Users/Details.aspx?uid=" + arr[0] + "&vid=" + arr[1]);

            }

            if (e.CommandName == "DeductList")//扣分
            {
                //选中即跳往相关人员列表
                Response.Redirect("DeductList.aspx?vid=" + e.CommandArgument);

            } 
        }
    }
    
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        if (Session["Permission"] != null)
        {
            Permission permission = Session["Permission"] as Permission;

            if (!string.IsNullOrEmpty(txtKeyWord.Text) || !string.IsNullOrEmpty(txtSearchDate.Text))
            {
                Bind(permission);
            }
        }
    }
}
